﻿using Dyao.Book.Contract;
using Dyao.Book.Repository.Abstractions;
using Foundation.Core;

namespace Dyao.Book.Application
{
    public abstract class BaseBookContentPlugins : ITransientDependency
    {
        protected readonly IBookCatalogueRepository _catalogueRepository;

        /// <summary>
        /// Key
        /// </summary>
        public abstract string SourceKey { get; set; }

        public BaseBookContentPlugins(IBookCatalogueRepository catalogueRepository)
        {
            _catalogueRepository = catalogueRepository;
        }

        /// <summary>
        /// 获取未爬取的章节
        /// </summary>
        /// <param name="bookId"></param>
        /// <param name="cancellationToken"></param>
        public async Task<List<BookNotCrawlChapterDto>> GetNotCrawlCompalteChaptersAsync(string bookId, CancellationToken cancellationToken)
        {
            var chapters = await _catalogueRepository.GetNotCrawlCompalteByBookIdAsync(bookId,
                bc => new BookNotCrawlChapterDto()
                {
                    Id = bc.ID,
                    ContentLink = bc.ContentLink,
                    SourceKey = bc.SourceKey,
                }, cancellationToken);

            return chapters;
        }
        /// <summary>
        /// 获取书源配置
        /// </summary>
        /// <param name="args">章节插件参数</param>
        /// <returns></returns>
        public BookSourceWarehouseDto GetSourceSetting(BookContentPluginsArgs args)
        {
            var setting = args.SourceSettings.FirstOrDefault(s => s.SourceKey == SourceKey);
            return setting;
        }


    }
}
